﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WorkflowActions;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint.Workflow;
using System.ComponentModel;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.SharePoint.Utilities;

namespace UpdateMultipleListItemsSPDActivity
{
    [ActivityToolboxDisplay("Custom Activities", true)]
    public class UpdateMultipleListItemsActivity : SequenceActivity
    {
        public static readonly DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }

        public static readonly DependencyProperty ListNameProperty = DependencyProperty.Register("ListName", typeof(string), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ListName
        {
            get
            {
                return (string)base.GetValue(ListNameProperty);
            }
            set
            {
                base.SetValue(ListNameProperty, value);
            }
        }

        public static readonly DependencyProperty WebUrlProperty = DependencyProperty.Register("WebUrl", typeof(string), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string WebUrl
        {
            get
            {
                return (string)base.GetValue(WebUrlProperty);
            }
            set
            {
                base.SetValue(WebUrlProperty, value);
            }
        }

        public static readonly DependencyProperty CAMLQueryProperty = DependencyProperty.Register("CAMLQuery", typeof(string), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string CAMLQuery
        {
            get
            {
                return (string)base.GetValue(CAMLQueryProperty);
            }
            set
            {
                base.SetValue(CAMLQueryProperty, value);
            }
        }

        public static readonly DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName", typeof(string), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string FieldName
        {
            get
            {
                return (string)base.GetValue(FieldNameProperty);
            }
            set
            {
                base.SetValue(FieldNameProperty, value);
            }
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(UpdateMultipleListItemsActivity));
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Value
        {
            get
            {
                return (string)base.GetValue(ValueProperty);
            }
            set
            {
                base.SetValue(ValueProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            ISharePointService wfService =
  executionContext.GetService<ISharePointService>();
            try
            {
                using (SPSite site = new SPSite(WebUrl))
                {
                    try
                    {
                        using (var web = site.OpenWeb())
                        {
                            SPList list = null;
                            try
                            {
                                list = web.Lists[ListName];
                            }
                            catch (Exception e1)
                            {
                                wfService.LogToHistoryList(executionContext.ContextGuid,
                                 SPWorkflowHistoryEventType.WorkflowComment,
                                 0,
                                 TimeSpan.Zero,
                                 "Error",
                                 "Unable to open list named '" + ListName + "'. Error: " + e1.ToString(),
                                 string.Empty);
                            }
                            SPField field = null;
                            try
                            {
                                field = list.Fields.GetField(FieldName);
                            }
                            catch (Exception e2)
                            {
                                wfService.LogToHistoryList(executionContext.ContextGuid,
                                 SPWorkflowHistoryEventType.WorkflowComment,
                                 0,
                                 TimeSpan.Zero,
                                 "Error",
                                 "Unable to find field named '" + FieldName + "'. Error: " + e2.ToString(),
                                 string.Empty);
                            }
                            var query = new SPQuery();
                            query.Query = HttpUtility.HtmlDecode(CAMLQuery);
                            SPListItemCollection items = null;
                            try
                            {
                                items = list.GetItems(query);
                            }
                            catch (Exception e3)
                            {
                                wfService.LogToHistoryList(executionContext.ContextGuid,
                                 SPWorkflowHistoryEventType.WorkflowComment,
                                 0,
                                 TimeSpan.Zero,
                                 "Error",
                                 "Unable to execute query '" + CAMLQuery + "'. Error: " + e3.ToString(),
                                 string.Empty);
                            }
                            foreach (SPListItem item in items)
                            {
                                try
                                {
                                    item[field.Id] = Value;
                                    item.Update();
                                }
                                catch (Exception e4)
                                {
                                    wfService.LogToHistoryList(executionContext.ContextGuid,
                                     SPWorkflowHistoryEventType.WorkflowComment,
                                     0,
                                     TimeSpan.Zero,
                                     "Error",
                                     "Unable to update item with id '" + item.ID + "'. Error: " + e4.ToString(),
                                     string.Empty);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        wfService.LogToHistoryList(executionContext.ContextGuid,
                     SPWorkflowHistoryEventType.WorkflowComment,
                     0,
                     TimeSpan.Zero,
                     "Error",
                     "Unable to open website at url '" + WebUrl + "'. Error: " + e.ToString(),
                     string.Empty);
                    }
                }
            }
            catch (Exception ex)
            {
                wfService.LogToHistoryList(executionContext.ContextGuid,
                     SPWorkflowHistoryEventType.WorkflowComment,
                     0,
                     TimeSpan.Zero,
                     "Error",
                     "Unable to open site collection at url '" + WebUrl + "'. Error: " + ex.ToString(),
                     string.Empty);
            }
            return base.Execute(executionContext);
        }
    }
}